package com.example.demo.config;

import com.example.demo.common.ApplicationVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    /**
     *   判断用户登录
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(ApplicationVariable.SESSION_USERINFO_KEY) != null){
            return true;
        }
        //用户未登录
        response.sendRedirect("/login.html");
        return  false;
    }
}
